/**
 * 例子
 * @author luguoxiang
 * @date 2023-03-18
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { IConfigSchema } from '@/soar';
import api from './api';
import { basePath } from './api';
import { basePath as itemPath } from './item/api';
import { MenuComponent } from '@/types/interface';

const fieldList: IConfigSchema<any>[] = [
  {
    field: 'name',
    label: '名称',
    dataType: 'string',
    form: {
      rules: true,
    },
  },
  {
    field: 'code',
    label: 'code',
    dataType: 'string',
    form: {
      rules({ data, isUpdate }) {
        const { id } = data;
        if (isUpdate && !id) {
          return [];
        }
        return [
          { required: true, message: '必填' },
          // eslint-disable-next-line consistent-return
          api.createValidator((val) => {
            if (val) {
              return { $soarQuery: { 'id-ne': id, 'code-eq': val } };
            }
          }),
        ];
      },
    },
  },
  {
    field: 'remark',
    label: '备注',
    dataType: 'string',
    form: {
      component: 't-textarea',
    },
  },
  {
    field: 'enumerated',
    label: '是否是枚举值',
    dataType: 'boolean',
    table: {
      width: 150,
    },
    form: false,
  },
  {
    field: 'disabled',
    label: '是否禁用',
    dataType: 'boolean',
    table: {
      width: 150,
    },
  },
];

export default fieldList;

/**
 * 组件配置（用于生成菜单数据）
 */
export const COMPONENTS: MenuComponent[] = [
  {
    title: '数据字典',
    component: () => import('./List.vue'),
    sort: 10,
    keepalive: true,
    icon: 'view-module',
    permits: [`GET${basePath}/query`, `GET${itemPath}/query`],
    elements: [
      { title: '新增', code: 'create', permits: [`POST${basePath}/create`, `POST${itemPath}/create`] },
      { title: '更新', code: 'update', permits: [`POST${basePath}/update`, `GET${basePath}/detail`, `POST${itemPath}/update`, `GET${itemPath}/detail`] },
      { title: '删除', code: 'remove', permits: [`POST${basePath}/remove`, `POST${itemPath}/remove`] },
      { title: '详情', code: 'detail', permits: [`GET${basePath}/detail`, `GET${itemPath}/detail`] },
      { title: '导入', code: 'import', permits: [`POST${basePath}/import`, `POST${itemPath}/import`] },
      { title: '导出', code: 'export', permits: [`GET${basePath}/export`, `GET${itemPath}/export`] },
      { title: '清除缓存', code: 'clear-cache', permits: [`POST${basePath}/clear-cache`] },
      { title: '扫描枚举', code: 'scan', permits: [`POST${basePath}/scan`] },
    ],
  },
];
